package com.suyuan.service.impl;

import com.suyuan.dao.ProPicMapper;
import com.suyuan.entity.ProPic;
import com.suyuan.service.PictureService;
import com.suyuan.service.ProPicService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
import java.util.UUID;

/**
 * 保存图片工具类
 *
 */
@Service(value = "pictureService")
@Transactional
public class PictureServiceImpl implements PictureService{

    @Resource
    private ProPicMapper proPicMapper;

    /**
     * 保存图片工具类，返回图片保存链接(/images/****.png)
     * @param
     * @return
     */
    public boolean savePic(Map<String, MultipartFile> fileMap, HttpServletRequest request,Integer productId)throws Exception{
        String realPath = request.getServletContext().getRealPath("/WEB-INF/images/");

        for(Map.Entry<String, MultipartFile> entry : fileMap.entrySet()){
            System.out.println(entry.getKey() + ":" + entry.getValue().getOriginalFilename());
            MultipartFile mutiFile = entry.getValue();
            String key = entry.getKey();//文件的name值
            if(mutiFile != null && StringUtils.isNotBlank(FilenameUtils.getExtension(mutiFile.getOriginalFilename()))){//没有后缀名的图片为空
                //设置文件新的名称
                String uuid = UUID.randomUUID().toString().replace("-","");
                String extension = FilenameUtils.getExtension(mutiFile.getOriginalFilename());
                String newFileName = uuid + "." + extension;
                //不是缩略图才进行文件转存，否则可能出现转存两次报错
                if(!"littlePic".equals(key)){
                    mutiFile.transferTo(new File(realPath+"/"+newFileName));
                }

                ProPic pic = new ProPic();
                pic.setProductId(productId);
                pic.setUrl("/images/"+newFileName);
                if(key.contains("rollPic")){//rollPic1、rollPic2、rollPic3、rollPic4
                    pic.setType(2);
                }
                if(key.contains("detailPic")){//detailPic1、detailPic2
                    pic.setType(1);
                }
                if(pic.getType() != null){//这儿判断空是为了防止把缩略图也加进去了
                    proPicMapper.insert(pic);
                }
            }
        }
        return true;
    }

    public String saveLittlePic(MultipartFile littlePic , HttpServletRequest request)throws Exception{
        //全路径
        String realPath = request.getServletContext().getRealPath("/WEB-INF/images/");
        //设置文件新的名称
        String uuid = UUID.randomUUID().toString().replace("-","");
        String extension = FilenameUtils.getExtension(littlePic.getOriginalFilename());
        String newFileName = uuid + "." + extension;
        littlePic.transferTo(new File(realPath+"/"+newFileName));
        return "/images/"+newFileName;
    }

}
